package demo;

import java.util.LinkedList;
import java.util.List;

/**
 *   螺旋矩阵
 */
class Solution25 {
	public List<Integer> spiralOrder(int[][] matrix) {
		List<Integer> result = new LinkedList<>();
		if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
			return result;
		}
		int left = 0;
		int right = matrix[0].length - 1;
		int up = 0;
		int down = matrix.length - 1;
		while (left <= right && up <= down) {
			for (int i = left; i <= right; i++) {
				result.add(matrix[up][i]);
			}
			up++;
			for (int i = up; i <= down; i++) {
				result.add(matrix[i][right]);
			}
			right--;
			for (int i = right; up < down && i >= left; i--) {
				result.add(matrix[down][i]);
			}
			down--;
			for (int i = down; i >= up && right >= left; i--) {
				result.add(matrix[i][left]);
			}
			left++;
		}
		return result;
	}
}

